﻿<?xml version="1.0" encoding="utf-8" ?>

<cls:class
	name="IdeCdDriveReader"
	type="http://metalx.org/Storage/Ide/IdeCdDriveReader"
	xmlns="http://metalx.org/Program"
	xmlns:dbg="http://metalx.org/i386/Functions/Debug"
	xmlns:lst="http://metalx.org/i386/Functions/List"
	xmlns:con="http://metalx.org/i386/Functions/Console"
	xmlns:int="http://metalx.org/i386/Functions/Integer"
	xmlns:byte="http://metalx.org/i386/Functions/Byte"
	xmlns:bool="http://metalx.org/i386/Functions/Boolean"
	xmlns:str="http://metalx.org/i386/Functions/String"
	xmlns:clsf="http://metalx.org/i386/Functions/Class"
	xmlns:mm="http://metalx.org/i386/Functions/MemoryManager"
	xmlns:var="http://metalx.org/Variable"
	xmlns:sys="http://metalx.org/i386/Functions/System"
	xmlns:cat="http://metalx.org/i386/Functions/Catalog"
	xmlns:obj="http://metalx.org/i386/Functions/Object"
	xmlns:cd="http://metalx.org/i386/Functions/CdDrive"
	xmlns:asc="http://metalx.org/Ansi/Ascii"
	xmlns:ari="http://metalx.org/Intel/80386/Arithmetic"
	xmlns:logic="http://metalx.org/Intel/80386/Logic"
	xmlns:math="http://metalx.org/Intel/80386/Math"
	xmlns:shift="http://metalx.org/Intel/80386/Shift"
	xmlns:imm="http://metalx.org/Intel/80386/Immediate"
	xmlns:inc="http://metalx.org/Intel/80386/Increment"
	xmlns:index="http://metalx.org/Intel/80386/Index"
	xmlns:cpu="http://metalx.org/Intel/80386/Operators"
	xmlns:op="http://metalx.org/Intel/80386/Operands"
	xmlns:cls="http://metalx.org/Class">

	<cls:field name="CdDrive" offset="0" type="http://metalx.org/Reference"/>
	<cls:field name="Position" offset="4" type="http://metalx.org/Integer"/>

	<cls:method name="Create" type="http://metalx.org/Storage/Ide/IdeCdDriveReader" static="true">
		<cls:parameter name="CdDrive" type="http://metalx.org/Storage/Ide/IdeCdDrive"/>
		<cls:parameter name="Position" type="http://metalx.org/Integer"/>

		<mm:CheckOut length="8"/>

		<cpu:CopyOperandToRegister/>
		<op:AX-BPAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:CopyRegisterToOperand/>
		<op:AX-DIAddress/>

		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<obj:GetData/>

		<cpu:CopySIAddressToAXAndIncrementSI/>

		<cpu:PushAXToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-DIAddressPlusImmediate8/>
		<byte>4</byte>

		<obj:Create length="8"/>

		<cpu:PullAXFromStack/>

		<cpu:AndAXWithImmediate/>
		<hex>000007ff</hex>

		<cpu:BranchToRelative8IfZero/>
		<addressOf ref="skipReadBlock" type="Relative8"/>

		<cpu:CallRelative/>
		<addressOf ref="readBlock" type="Relative32"/>

		<label id="skipReadBlock"/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>8</byte>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="SetPosition" type="http://metalx.org/Storage/Ide/IdeCdDriveReader">
		<cls:parameter name="Position" type="http://metalx.org/Integer"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<obj:GetData/>

		<cpu:CopySIAddressToAXAndIncrementSI/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:BX-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:DecrementBX/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftBXRegisterRight/>
		<byte>11</byte>

		<cpu:CopyRegisterToOperand/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:DecrementAX/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftAXRegisterRight/>
		<byte>11</byte>

		<cpu:CompareRegisterToOperand/>
		<op:AX-BXRegister/>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="sameBlock" type="Relative8"/>

		<cpu:CallRelative/>
		<addressOf ref="readBlock" type="Relative32"/>

		<label id="sameBlock"/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="Skip" type="http://metalx.org/Storage/Ide/IdeCdDriveReader">
		<cls:parameter name="Position" type="http://metalx.org/Integer"/>
		
		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<obj:GetData/>

		<cpu:CopySIAddressToAXAndIncrementSI/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:BX-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:AddRegisterToOperand/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:AddRegisterToOperand/>
		<op:BX-AXRegister/>

		<cpu:DecrementAX/>
		<cpu:DecrementBX/>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftBXRegisterRight/>
		<byte>11</byte>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftAXRegisterRight/>
		<byte>11</byte>

		<cpu:CompareRegisterToOperand/>
		<op:AX-BXRegister/>

		<cpu:BranchToRelative8IfEqual/>
		<addressOf ref="sameBlock" type="Relative8"/>

		<cpu:CallRelative/>
		<addressOf ref="readBlock" type="Relative32"/>

		<label id="sameBlock"/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadObject" type="http://metalx.org/Storage/Ide/IdeCdDriveReader">
		<cls:parameter name="Object" type="http://metalx.org/Object"/>

		<cpu:PushDIToStack/>

		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<obj:GetDataLength/>

		<obj:GetData/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-CXRegister/>

		<label id="nextByte"/>

		<cpu:PushCXToStack/>
		<cpu:PushDIToStack/>

		<cpu:CopyOperandToRegister/>
		<op:DI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>8</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:PullDIFromStack/>

		<cpu:CopyALToDIAddressAndIncrementDI/>

		<cpu:PullCXFromStack/>

		<cpu:LoopToRelative8/>
		<addressOf ref="nextByte" type="Relative8"/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<cpu:PullDIFromStack/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadBytes" type="http://metalx.org/List">
		<cls:parameter name="Count" type="http://metalx.org/Integer"/>

		<cpu:CopyImmediateToDI/>
		<int>0</int>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadBoolean" type="http://metalx.org/Boolean">
		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<bool:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadByte" type="http://metalx.org/Byte">
		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<byte:CreateObject/>
		
		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadShort" type="http://metalx.org/Short">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>
		
		<mm:CheckOut length="2"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>
		
		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-SIAddress/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:PullDIFromStack/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-DIAddressPlusImmediate8/>
		<byte>1</byte>

		<obj:Create length="2" type="http://metalx.org/Short"/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadInteger" type="http://metalx.org/Integer">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<mm:CheckOut length="4"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-SIAddress/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-SIAddressPlusImmediate8/>
		<byte>1</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-SIAddressPlusImmediate8/>
		<byte>2</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:PullDIFromStack/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-DIAddressPlusImmediate8/>
		<byte>3</byte>

		<obj:Create length="4" type="http://metalx.org/Integer"/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadFloat" type="http://metalx.org/Float">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<mm:CheckOut length="4"/>

		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-SIAddress/>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-SIAddressPlusImmediate8/>
		<byte>1</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:CopyOperandToRegister/>
		<op:SI-IndexAddress/>
		<index:SP/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-SIAddressPlusImmediate8/>
		<byte>2</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:PullDIFromStack/>

		<cpu:CopyRegisterToOperand8/>
		<op:AL-DIAddressPlusImmediate8/>
		<byte>3</byte>

		<obj:Create length="4" type="http://metalx.org/Float"/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadDouble" type="http://metalx.org/Double">
		<cpu:CopyImmediateToDI/>
		<int>0</int>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadReal" type="http://metalx.org/Real">
		<cpu:CopyImmediateToDI/>
		<int>0</int>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ReadString" type="http://metalx.org/String">
		<cls:parameter name="Count" type="http://metalx.org/Integer"/>

		<cpu:PushDIToStack/>
		
		<cpu:CopyOperandToRegister/>
		<op:SI-BPAddressPlusImmediate8/>
		<byte>0</byte>

		<obj:GetData/>

		<cpu:CopySIAddressToAXAndIncrementSI/>

		<cpu:PushAXToStack/>

		<str:Create/>

		<cpu:PullAXFromStack/>

		<cpu:PushDIToStack/>

		<cpu:CopyAXToDIAddressAndIncrementDI/>

		<cpu:CopyRegisterToOperand/>
		<op:AX-CXRegister/>

		<label id="nextCharacter"/>

		<cpu:PushCXToStack/>
		<cpu:PushDIToStack/>

		<cpu:CopyOperandToRegister/>
		<op:DI-IndexAddressPlusImmediate8/>
		<index:SP/>
		<byte>12</byte>

		<cpu:CallRelative/>
		<addressOf ref="readByte" type="Relative32"/>

		<cpu:PullDIFromStack/>

		<cpu:CopyALToDIAddressAndIncrementDI/>

		<cpu:PullCXFromStack/>

		<cpu:LoopToRelative8/>
		<addressOf ref="nextCharacter" type="Relative8"/>

		<cpu:PullDIFromStack/>

		<str:CreateObject/>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToBPRegister/>
		<byte>4</byte>

		<cpu:MathImmediate8ToOperandFunction/>
		<math:AddToSPRegister/>
		<byte>4</byte>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="ToString" type="http://metalx.org/String">
		<cpu:CopyImmediateToDI/>
		<addressOf ref="className" type="Absolute32"/>

		<str:CreateObject/>

		<cpu:ReturnToNearCaller/>

		<var:string id="className">IDE CD Drive Reader</var:string>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class">
		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetType/>

		<cpu:CopyRegisterToOperand/>
		<op:SI-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<cls:method name="GetClass" type="http://metalx.org/Class" static="true">
		<cpu:CopyRegisterToOperand/>
		<op:BX-DIRegister/>

		<clsf:CreateObject/>

		<cpu:ReturnToNearCaller/>
	</cls:method>

	<label id="readByte">
		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:IncrementFunction/>
		<inc:IncrementSIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:AndAXWithImmediate/>
		<hex>000007ff</hex>

		<cpu:BranchToRelative8IfNotZero/>
		<addressOf ref="skipReadBlock2" type="Relative8"/>

		<cpu:PushAXToStack/>

		<cpu:CallRelative/>
		<addressOf ref="readBlock" type="Relative32"/>

		<cpu:PullAXFromStack/>

		<label id="skipReadBlock2"/>

		<cpu:AddImmediateToAX/>
		<hex>00008000</hex>

		<cpu:CopyOperandToRegister/>
		<op:AL-AXAddress/>

		<cpu:PullDIFromStack/>

		<cpu:ReturnToNearCaller/>
	</label>

	<label id="readBlock">
		<cpu:PushDIToStack/>

		<cpu:CopyRegisterToOperand/>
		<op:DI-SIRegister/>

		<obj:GetData/>

		<cpu:CopyOperandToRegister/>
		<op:AX-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:ShiftRegisterByImmediate8Function/>
		<shift:ShiftAXRegisterRight/>
		<byte>11</byte>

		<cpu:CopyOperandToRegister/>
		<op:SI-SIAddress/>

		<obj:GetData/>

		<!--<cpu:PushAXToStack/>
		
		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddressPlusImmediate8/>
		<byte>4</byte>

		<cpu:CopyImmediateToAL/>
		<binary>00000100</binary>

		<cpu:WriteALToDXPort/>

		<cpu:ReadFromDXPortToAL/>
		<cpu:ReadFromDXPortToAL/>
		<cpu:ReadFromDXPortToAL/>
		<cpu:ReadFromDXPortToAL/>

		<cpu:CopyImmediateToAL/>
		<binary>00000000</binary>

		<cpu:WriteALToDXPort/>

		<cpu:ReadFromDXPortToAL/>
		<cpu:ReadFromDXPortToAL/>
		<cpu:ReadFromDXPortToAL/>
		<cpu:ReadFromDXPortToAL/>

		<cpu:PullAXFromStack/>-->

		<cpu:CopyOperandToRegister/>
		<op:DX-SIAddress/>

		<cpu:CopyOperandToRegister8/>
		<op:BL-SIAddressPlusImmediate8/>
		<byte>8</byte>

		<cpu:MathImmediateToOperandFunction/>
		<math:AndWithBXRegister/>
		<hex>000000ff</hex>

		<cpu:CopyImmediateToDI/>
		<hex>00008000</hex>

		<cd:ReadBlock/>

		<cpu:PullDIFromStack/>

		<cpu:ReturnToNearCaller/>
	</label>
</cls:class>